<% session.setAttribute("title", "Upload Thesis"); %> 
<%@ include file="Header.jsp"%>
<%
	String error = "";
	if (request.getParameter("upload") != null) 
		if (request.getParameter("graduation_date").equals("") 
			   || request.getParameter("file_location").equals("")  
			    || request.getParameter("department").equals("")
			     || request.getParameter("faculty").equals("")
			      		|| session.getAttribute("username").equals("")
		){
			error = "There are errors in your upload";
		}
%>

<%
	org.jbpm.api.ProcessEngine processEngine = StaticProcessEngine
			.getInstance();
	TaskService taskService = processEngine.getTaskService();

	if (request.getParameter("upload") != null && error.equals("")) {
		String taskId = request.getParameter("task_id");
		Set<String> variableNames = taskService
				.getVariableNames(taskId);
		Map<String, Object> variables = taskService.getVariables(
				taskId, variableNames);
		variables.put("graduation_date", request
				.getParameter("graduation_date"));
		variables.put("file_location", request
				.getParameter("file_location"));
		variables.put("department", request.getParameter("department"));
		variables.put("faculty", request.getParameter("faculty"));
		taskService.completeTask(taskId, variables);

	} 

		List<Task> tasks = taskService.findPersonalTasks(session.getAttribute("username").toString());

		if (tasks.isEmpty()) {
			%>No thesis waiting for upload for you<%
		}
		
		if (request.getParameter("upload") != null && !error.equals("")) {
			%><span style="color: red;"><% out.println(error); %></span><br /><%
		}

		for (Task task : tasks) {
			String taskId = task.getId();
			Set<String> variableNames = taskService.getVariableNames(taskId);
			Map<String, Object> variables = taskService.getVariables(taskId, variableNames);
		%>
		<br />
		Proposal title:
		<%=variables.get("title")%><br />
		<form action="UploadThesisPage.jsp">
			<table cellpadding="5" cellspacing="5">
				<tr>
					<td>Graduation Date: </td>
					<td><input type="text" name="graduation_date" /></td>
				</tr>
				<tr> 
					<td>File: </td>
					<td><input type="text" name="file_location" /></td>
				</tr>
				<tr>
					<td>Department:</td>
					<td><input type="text" name="department" /></td>
				</tr>
				<tr>
					<td>Faculty: </td>
					<td><input type="text" name="faculty" /></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" name="upload" value="Upload" /> </td>
				</tr>
				<input type="hidden" name="task_id" value="<%=taskId%>" /> 
			</table>
		</form>
		<% } %>
<%@ include file="Footer.jsp"%>